<template>
	<view class="home-wrap u-m-b-20">
		<base-nav-bar :backIconColor="'#333'" :isBack="true" :background="{'background':'#fff'}">
			<text
				slot="content"
				style="color: #333; width: 100%; text-align: center;padding-left: 20rpx;"
			>
				购买课程包
			</text>
		</base-nav-bar>
		<view class="content-box u-p-20 u-m-l-40 u-m-r-40">
			<!-- grid商家功能模块 -->
			<view class="u-flex u-flex-items-center u-flex-start u-flex-wrap">
				<u--form :model="form" ref="uForm" :labelPosition="'top'" style="width: 100%;" :labelStyle="{'margin-botttom':'10rpx'}">
<!--					<u-form-item label="手机号" prop="phone" labelWidth="120">-->
<!--						<u-input v-model="form.phone" placeholder="请输入手机号" @blur="getScore()"/>-->
<!--					</u-form-item>-->
					<u-form-item label="选择用户" labelWidth="120"
					             prop="user_name"
					             @click="openUser()">
						<u-input
							v-model="form.user_name"
							placeholder="请选择用户"
							suffixIcon="arrow-right"
							suffixIconStyle="color: #909399"
							readonly
						></u-input>
					</u-form-item>
					<u-form-item label="选择课程包" labelWidth="120"
					             prop="yueka"
					             ref="item2"
					             @click="openClass()">
						<u-input
							v-model="form.course_name"
							placeholder="选择赠送的课程包"
							suffixIcon="arrow-right"
							suffixIconStyle="color: #909399"
							readonly
						></u-input>
					</u-form-item>
					<u-form-item label="课时" prop="fixed_score" labelWidth="120">
						<u-input v-model="form.score" placeholder="请输入可预约上课的节数"/>
					</u-form-item>
					<u-form-item label="课程包价格(元)" prop="price" labelWidth="120">
						<u-input v-model="form.price" placeholder="请输入课程价格"/>
					</u-form-item>
<!--					<u-form-item label="请上传支付凭证" prop="name" labelWidth="120">-->
<!--						<u-upload-->
<!--							:fileList="rechargeImages"-->
<!--							@afterRead="afterRead"-->
<!--							@delete="deletePic"-->
<!--							name="Images"-->
<!--							multiple-->
<!--							:maxCount="2"-->
<!--							:previewFullImage="true"-->
<!--						></u-upload>-->
<!--					</u-form-item>-->
				</u--form>
				<view class="submit-btn" @click="submit">提交</view>
			</view>
		</view>
		<u-picker :show="showClass" :columns="courseList" :title="'选择月卡课程'"  key-name="name" @confirm="chooseClass($event)" @cancel="closeClass()"></u-picker>
		<shopro-search-picker :show="showUser" @confirm="confirm" @close="closeUser()"></shopro-search-picker>
	</view>
</template>

<script>

import { mapGetters } from 'vuex';
import { HTTP_IMG_URL } from '../../config/app.js'
import BaseNavBar from "../../components/BaseNavBar.vue";

export default {
	components: {
		BaseNavBar
	},
	data() {
		return {
			scoreAll:'',
			showType:false,
			showClass:false,
			showUser:false,
			form: {
				user_name: '',
				score:'',
				fixed_score:'',
				yueka:'',
				yueType:'',
				type:'',
				chongName:'',
				card_daychou:Number(new Date()),
				card_day:'',
				course_id:'',
				course_name:''
			},
			rules: {
				user_name: [
					{
						required: true,
						message: '请选择用户',
						trigger: ['blur', 'change'],
					},
				],
				type: [
					{
						required: true,
						message: '请选择充值类型',
						trigger: ['blur', 'change']
					}
				],
			},
			rechargeImages:[],
			columns: [
				['中国', '美国', '日本']
			],
			chongClosable:false,
			chongTypeList:[
				[
					{
						name:'积分充值',
						id:'1'
					},
					{
						name:'月卡充值',
						id:'2'
					}
				]
			],
			yueTypeList:[[
				"1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",
				"9", "10", "11", "12", "13", "14", "15", "16",
				"17", "18", "19", "20", "21", "22", "23", "24"
			]],
			courseList:[]
		};
	},
	computed: {
		...mapGetters(['isLogin','userInfo']),
		
	},
	onPullDownRefresh() {
	
	},
	
	onShow() {
	},
	onHide() {
	},
	onLoad() {
		this.getCardList();
	},
	onReady() {
		//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		this.$refs.uForm.setRules(this.rules)
	},
	methods: {
		chooseClass(e){
			this.showClass = false;
			this.form.course_id = e.value[0].id
			this.form.course_name = e.value[0].name
		},
		openClass(){
			this.showClass = true;
			console.log("====this.showType",this.showClass)
		},
		closeClass(){
			this.showClass = false;
		},
		HTTP_IMG_URL() {
			return HTTP_IMG_URL
		},
		// 路由跳转
		jump(path, parmas) {
			uni.navigateTo({
				url: path,
				query: parmas
			});
		},
		submit() {
			let imageArr = [];
			if(this.rechargeImages.length>0){
				this.rechargeImages.forEach(item=>{
					imageArr.push(item.url)
				})
			}
			this.$refs.uForm.validate().then(res => {
				let fromData = {
					...this.form,
					image:imageArr.join(',')
				}
				this.$Api.merchant.rechargeAdd(fromData).then(res=>{
					uni.$u.toast(res.msg)
					if(res.code == 1){
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},500)
					}
				}).catch(err=>{
					console.log(e)
				})
			}).catch(errors => {
				uni.$u.toast('校验失败')
			})
		},
		// 删除图片
		deletePic(event) {
			this[`recharge${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`recharge${event.name}`].length
			for (let i = 0; i < lists.length; i++) {
				const result = await this.$uploadFilePromise(lists[i].url)
				this[`recharge${event.name}`].splice(fileListLen, 1, {
					status: 'success',
					message: '',
					url: result.fullurl
				})
				fileListLen++
			}
		},
		getCardList(){
			var that = this;
			that.courseList = [];
			that.$Api.merchant.allcourseList().then(res=>{
				that.courseList.push(res.data);
				console.log("111=====this.courseList",that.courseList)
			})
		},
		openUser(){
			this.showUser = true;
			console.log("====this.showType",this.showUser)
		},
		closeUser(){
			this.showUser = false;
		},
		confirm(id,name){
			console.log("====id,name",id,name);
			this.course_name = name;
			this.course_id = id;
			this.showUser = false;
		},
		close(){
			this.showUser = false;
		}
	}
};
</script>

<style lang="scss" scoped>
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
.home-wrap {
	min-height: 100vh;
	background: #F6F6F8;
	position: relative;
}
.kehu-item {
	border-radius: 20rpx;
	width: 100%;
	.swipers{
		width: 100%;
	}
}
.nickname{
	font-size: 34rpx;
	font-weight: bold;
}
.content-box{
	background: #FFFFFF;
	border-radius: 30rpx;
	min-height: 80vh;
	width: calc( 100% - 76rpx );
	position: absolute;
	top: 180rpx;
	z-index: 20;
	.pay-item{
		width: 25%;
	}
}
::v-deep .u-form{
	width: 100%;
}
.images-content{
	width: 100%;
	height: 300rpx;
	display: flex;
	border:1rpx solid #eee;
}
.merchant-title {
	position: absolute;
	top: 190rpx;
	width: calc( 100% - 76rpx );
	height: 54px;
	background: #C9EFD0;
	border-radius: 18px 18px 0px 0px;
	.merchant-main-title {
		color: #30AA4D;
		text-align: center;
	}
	
	.merchant-sub-title {
		color: #30AA4D;
		.merchant-sub-title-btn{
			color: #30AA4D;
		}
	}
}
.submit-btn{
	margin-top: 20rpx;
	width: 100%;
	height: 84rpx;
	background: linear-gradient( #30AA4D 0%, #20CE83 100%);
	box-shadow: 0px 2px 4px 0px rgba(48,170,77,0.1);
	border-radius: 8px;
	display: flex;
	justify-content: center;
	align-items: center;
	color: #fff;
	font-size: 28rpx;
}
</style>
